package com.ge.healthcare.autosc.processor.dispatcher.tasks;

import java.util.ArrayList;
import java.util.List;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.dao.IModelTypePropDAO;
import com.ge.healthcare.autosc.common.database.dao.IRequestDAO;
import com.ge.healthcare.autosc.common.database.pojo.ActRequest;
import com.ge.healthcare.autosc.common.database.pojo.ActTask;
import com.ge.healthcare.autosc.common.database.pojo.IActTask;
import com.ge.healthcare.autosc.common.util.ApplicationInfo;
import com.ge.healthcare.autosc.common.util.PropertyConstants;
import com.ge.healthcare.autosc.pss.request.PSSRequest;
import com.ge.healthcare.autosc.pss.response.PSSResponse;
import com.ge.healthcare.autosc.pssClient.TransportMgr;

public class ScheduleSweepPostIPTDS extends BasicTask implements
		IScheduleSweepPostIPTDS {
	protected IActTask task = null;
	protected ActRequest request = null;
	private PSSResponse pssResponse;
	private PSSRequest pssRequest;
	private static IRequestDAO requestDAO = null;
	private static IModelTypePropDAO modelTypePropDAO;
	// private static final String SWEEP_NAME = "prodiagsScheduleUpdate";
	private static final String PROGRAM_NAME = "ProDiags_PHI_Fix";
	private static final String SWEEP_NAME = "ProDiags_Scheduler";
	private static final String FROM_EMAIL = "autosc@med.ge.com";
	public static final String IPTDS_CLEANUP_INVALID_JOBS = "IPTDS_CLEANUP_INVALID_JOBS";

	public void executeTask(ActTask task) {
		String methodName = "ScheduleSweepPostIPTDS";
		ASCLogger.debug(this.getClass(), methodName,
				"Starting ScheduleSweepPostIPTDS for task:" + task);
		this.task = task;
		try {
			ASCLogger.info(this.getClass(), methodName,
					"Scheduling Sweep Request");
			pssRequest = new PSSRequest();
			pssRequest.setRequestType(SWEEP_NAME);

			request = ScheduleSweepPostIPTDS.requestDAO.getByRequestID(task
					.getRequestId());
			List<String> sidList = new ArrayList<String>();
			sidList.add(request.getSystemId());
			pssRequest.setSystemIdLst(sidList);

			// WhiteList email
			pssRequest.setFromEmailAddress(FROM_EMAIL);
			pssRequest.setSentEmailNotifications(false);

			List<String> addressLst = new ArrayList<String>();
			addressLst.add(FROM_EMAIL);
			pssRequest.setReplyToAddressLst(addressLst);

			String xargsValue = modelTypePropDAO.getValueForProperty(
					request.getModality(), request.getProductLine(),
					request.getModelType(), IPTDS_CLEANUP_INVALID_JOBS);
			if (!ASCUtil.isNullOrEmpty(xargsValue)) {
				pssRequest.setXargs(xargsValue);
			}
			pssRequest.setProgram(PROGRAM_NAME);
			ASCLogger.info(this.getClass(), methodName, "Sending request:"
					+ pssRequest);
			this.pssResponse = TransportMgr.sendWsRequestToPssServer(
					pssRequest, getPSSWsURL());
			ASCLogger.info(this.getClass(), methodName, "Received Response:"
					+ pssResponse);
		} catch (Throwable exception) {
			ASCLogger.error(this.getClass(), methodName,
					exception.getMessage(), exception);
		}

	}

	/**
	 * Using this to verify that the sweep request was generated. Only for
	 * testing.
	 * 
	 * @return
	 */
	public PSSResponse getPssResponse() {
		return pssResponse;
	}

	public PSSRequest getPssRequest() {
		return pssRequest;
	}

	/**
	 * Figures the URL for PSS Server
	 * 
	 * @return
	 */
	private static String getPSSWsURL() {
		return ApplicationInfo.getInstance().getProperty(
				PropertyConstants.PSS_WS_URL);
	}

	/**
	 * @param requestDAO
	 *            the requestDAO to set
	 */
	public void setRequestDAO(IRequestDAO requestDAO) {
		ScheduleSweepPostIPTDS.requestDAO = requestDAO;
	}

	/**
	 * @param modelTypePropDAO
	 *            the modelTypePropDAO to set
	 */
	public void setModelTypePropDAO(IModelTypePropDAO modelTypePropDAO) {
		ScheduleSweepPostIPTDS.modelTypePropDAO = modelTypePropDAO;
	}

}
